<?php
class Functions{
	static function uploadImagem($from, $to, $name, $width = 0, $height = 0) {
		
		$image = WideImage::load ( $from );
		if ($width == 0 && $height == 0) {
			list ( $width, $height ) = getimagesize ( $from );
			$resized = $image->resize ( $width, $height, 'inside' );
		} else {
			$resized = $image->resize ( $width, $height, 'inside' );
		}
		
		$resized->saveToFile ( $to . $name );
		
		return $name;
	
	}

	static function removeCharacter($str, $enc = "UTF-8") {
		
		$acentos = array ('A' => '/&Agrave;|&Aacute;|&Acirc;|&Atilde;|&Auml;|&Aring;/', 'a' => '/&agrave;|&aacute;|&acirc;|&atilde;|&auml;|&aring;/', 'C' => '/&Ccedil;/', 'c' => '/&ccedil;/', 'E' => '/&Egrave;|&Eacute;|&Ecirc;|&Euml;/', 'e' => '/&egrave;|&eacute;|&ecirc;|&euml;/', 'I' => '/&Igrave;|&Iacute;|&Icirc;|&Iuml;/', 'i' => '/&igrave;|&iacute;|&icirc;|&iuml;/', 'N' => '/&Ntilde;/', 'n' => '/&ntilde;/', 'O' => '/&Ograve;|&Oacute;|&Ocirc;|&Otilde;|&Ouml;/', 'o' => '/&ograve;|&oacute;|&ocirc;|&otilde;|&ouml;/', 'U' => '/&Ugrave;|&Uacute;|&Ucirc;|&Uuml;/', 'u' => '/&ugrave;|&uacute;|&ucirc;|&uuml;/', 'Y' => '/&Yacute;/', 'y' => '/&yacute;|&yuml;/', 'a.' => '/&ordf;/', 'o.' => '/&ordm;/' );
		
		return preg_replace ( $acentos, array_keys ( $acentos ), htmlentities ( $str, ENT_NOQUOTES, $enc ) );
	}
	
	
	static function open_image ($file) {
		
		$im = @imagecreatefromjpeg($file);
		if ($im !== false) { return $im; }
		$im = @imagecreatefromgif($file);
		if ($im !== false) { return $im; }
		$im = @imagecreatefrompng($file);
		if ($im !== false) { return $im; }
		$im = @imagecreatefromgd($file);
		if ($im !== false) { return $im; }
		$im = @imagecreatefromgd2($file);
		if ($im !== false) { return $im; }
		$im = @imagecreatefromwbmp($file);
		if ($im !== false) { return $im; }
		$im = @imagecreatefromxbm($file);
		if ($im !== false) { return $im; }
		$im = @imagecreatefromxpm($file);
		if ($im !== false) { return $im; }
		$im = @imagecreatefromstring(file_get_contents($file));
		if ($im !== false) { return $im; }
		return false;
	}
	
	
	static function resizeImage($im, $file,$laMax, $alMax)
	{		
		$la="";
		$al="";
	
		//Seta todas as strings como 'string'
		settype($la, "string");
		settype($al, "string");
		
		// Carrega Imagem
		$image = open_image($im);	
		
		if ($image == false) {			
			die ('<strong>Erro ao abrir imagem</strong>');
		}		
		// Pega os tamanhos originais
		$width = imagesx($image);
		$height = imagesy($image);
		
		//echo "($width, $height)<br>";
		
		if($width > $height){
			if($laMax > $alMax){
				settype($al, "integer");
				$new_height = floatval($alMax);
				$new_width = $width * ($alMax/$height);
				//se a largura ainda ficar maior que o valor maximo, pega a diferença e diminui do valor final
				if($new_width > $laMax){
					$diferenca = $new_width - $laMax;
					$diferenca = abs($diferenca);
					//echo "<br>($new_height)| ";
					$new_height = $new_height - $diferenca;
					$new_height = abs($new_height);
					$new_width =  $new_width - $diferenca;
					//echo "($new_height)<br>";
				}
				//$new_width = round(($alMax*$width)/$height);
			}else{
				settype($la, "integer");
				$new_width = floatval($laMax);
				$new_height = $height * ($laMax/$width);
				//$new_height = round(($laMax*$height)/$width);
			}
		}else{
			if($alMax > $laMax){
				settype($la, "integer");
				$new_width = floatval($laMax);
				$new_height = $height * ($laMax/$width);
				//$new_height = round(($laMax*$height)/$width);
			}else{
				settype($al, "integer");
				$new_height = floatval($alMax);
				$new_width = $width * ($alMax/$height);
				//$new_width = round(($alMax*$width)/$height);
			}
		}
		
				
		$image_resized = imagecreatetruecolor($new_width, $new_height);	
		imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
		imagejpeg($image_resized,$im,100);		
		imagedestroy($image_resized);
	}
	
	static function validaVideo($link) {
		$pattern = '@((([?]v=)|(/\\d/)|(/v/))(.)*&)@';
		$validacao = strpos ( $link, '&' );
		
		if (! $validacao) {
			$saida = $link . '&';
		} else {
			$saida = $link;
		}
		preg_match_all ( $pattern, $saida, $match );
		
		if (isset ( $match [0] [0] )) {
			$strVideo = $match [0] [0];
			$tamanhoString = strlen ( $strVideo ) - 4;
			$strVideo = substr ( $strVideo, 3, $tamanhoString );
			return $strVideo;
		} else {
			return false;
		}
	}
	
		
	public static function removeAcentuacao($str, $enc = "UTF-8"){

		$acentos = array(
		'A' => '/&Agrave;|&Aacute;|&Acirc;|&Atilde;|&Auml;|&Aring;/',
		'a' => '/&agrave;|&aacute;|&acirc;|&atilde;|&auml;|&aring;/',
		'C' => '/&Ccedil;/',
		'c' => '/&ccedil;/',
		'E' => '/&Egrave;|&Eacute;|&Ecirc;|&Euml;/',
		'e' => '/&egrave;|&eacute;|&ecirc;|&euml;/',
		'I' => '/&Igrave;|&Iacute;|&Icirc;|&Iuml;/',
		'i' => '/&igrave;|&iacute;|&icirc;|&iuml;/',
		'N' => '/&Ntilde;/',
		'n' => '/&ntilde;/',
		'O' => '/&Ograve;|&Oacute;|&Ocirc;|&Otilde;|&Ouml;/',
		'o' => '/&ograve;|&oacute;|&ocirc;|&otilde;|&ouml;/',
		'U' => '/&Ugrave;|&Uacute;|&Ucirc;|&Uuml;/',
		'u' => '/&ugrave;|&uacute;|&ucirc;|&uuml;/',
		'Y' => '/&Yacute;/',
		'y' => '/&yacute;|&yuml;/',
		'a.' => '/&ordf;/',
		'o.' => '/&ordm;/');
		
		return preg_replace($acentos,array_keys($acentos),htmlentities($str,ENT_NOQUOTES, $enc));
    }
	
	public static function to_url($string){
		$string = html_entity_decode(functions::retira_acentos(str_replace(" ","-",trim(strtolower(strip_tags($string))))), ENT_QUOTES, "UTF-8");
		$string = preg_replace("/[^a-z0-9.-]+/i", "", $string);
		return $string;
	}
		
	/*
	 * Função para retornar urls encurtadas.
	 */
	public static function get_bitly_short_url($url,$login,$appkey,$format='txt') {
	  	$connectURL = 'http://api.bit.ly/v3/shorten?login='.$login.'&apiKey='.$appkey.'&uri='.urlencode($url).'&format='.$format;
	  return $this->curl_get_result($connectURL);
	}
		
	protected function curl_get_result($url) {
	  $ch = curl_init();
	  $timeout = 5;
	  curl_setopt($ch,CURLOPT_URL,$url);
	  curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
	  curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
	  $data = curl_exec($ch);
	  curl_close($ch);
	  return $data;
	}
	
	/*
	 * Função que recebe um link e adiciona http:// se for necessário.
	 */
	public static function trataLink($valor){
		if($valor!=""){
			$link = "";		
			if(substr($valor,0,4) == "http") {
	      		$link = $valor;      		
	      	}else{
	      		$link = "http://". $valor;      		
	      	}
	      	return $link;
		}
		return "";
	}		
}